<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<style type="text/css">
<!--
.cost_table {
	width: 90%; margin: 0 auto; padding: 5px; border-collapse: collapse; border-spacing: 0; border: 1px solid #999;
}

.cost_table th {
	
}

.cost_table td,.cost_table th {
	border: 1px solid #999; padding: 2px 3px;
}

.iscalculate {
	border: none !important; box-shadow: none !important;
}

.iscalculate,.isnotcalculate {
	
}
-->
</style>
<c:choose>
	<c:when test="${state eq 'view' }">
		<table class="form-table">
			<tr>
				<td class="td1">预算模板：</td>
				<td class="td2">${costTemplate.name }</td>
			</tr>
			<tr>
				<td class="td2" colspan="2">
					<table id="cost_table" class="cost_table">${projectCost }
					</table></td>
			</tr>
		</table>
	</c:when>
	<c:otherwise>
		<form id="formProjectCostItem"
			action="project/project/saveProjectCost">
			<input type="hidden" name="projectId" value="${projectId }">
			<div id="projCostTemplateDiv" style="padding:10px">
				<label style="line-height: 24px;vertical-align: top">请选择预算模板：</label>
				<form:select id="projCostTemplateId" name="projCostTemplateId"
					path="projCostTemplateId" items="${costTemplates }" itemValue="id"
					itemLabel="name" onchange="onTemplateSelected(this);">
				</form:select>
			</div>
			<table id="cost_table" class="cost_table">${projectCost }
			</table>
		</form>
		<script type="text/javascript">
			$("#projCostTemplateId").chosen({
				allow_single_deselect : true,
				width : "240px"
			});

			var selectedCostTemplate = "${projCostTemplateId}";

			if (selectedCostTemplate && selectedCostTemplate != "") {
				$("#projCostTemplateId").val(selectedCostTemplate);
				$("#projCostTemplateId").trigger("chosen:updated");
			}
		</script>
		<script type="text/javascript">
			var SUM = ${account};

			function cc() {
				var e_js = "";
				//从上到下，从左到右
				//找到所有手工填写的
				$(".isnotcalculate").each(function(index, item) {
					var $item = $(item);
					var d_code = $item.data("code");

					var i_v = $item.val();
					if (!i_v || i_v == "") {

						i_v = 0;
					}

					e_js += "var " + d_code + "=" + i_v + ";";
				});

				//组合所有的js
				$(".iscalculate").each(function(index, item) {
					var $item = $(item);
					var d_code = $item.data("code");
					var d_calculate = $item.data("calculate").replace(new RegExp("%", "gm"), "/100");

					e_js += "var " + d_code + "=" + d_calculate + ";";

				});

				//组合所有的js
				$(".iscalculate").each(function(index, item) {
					var $item = $(item);
					var d_code = $item.data("code");
					e_js += "$('#cost_" + d_code + "').val(" + d_code + ");";
				});

				eval(e_js);
			}

			cc();

			$(".isnotcalculate").change(function() {
				cc();
			});
		</script>


		<script type="text/javascript">
			function onTemplateSelected(obj) {

				var v = $(obj).val();
				if (v) {
					$.get("project/project/cost/costAccounting", {
						projectId : "${projectId }",
						projCostTemplateId : v
					}, function(data) {
						$("#cost_table").html(data);
						cc();
					});
				}
			}
		</script>

	</c:otherwise>
</c:choose>
